package com.dragon.manage.action; 

import java.util.Map; 

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; 

import com.dragon.manage.bean.BaseDTO;
import com.dragon.manage.util.RechargeUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.ModelDriven;

/**
* 所有Action的基类，
* 继承自BaseAction的action都可以直接使用HttpServletRequest,HttpServletResponse和Session
*/
public class BaseAction<DTO extends BaseDTO> extends ActionSupport implements SessionAware,
        ServletRequestAware, ServletResponseAware, ServletContextAware, ModelDriven<DTO> {

	private static final long serialVersionUID = 1L;

	protected Map<String, Object> session; 

    protected HttpServletRequest request; 

    protected HttpServletResponse response; 

    protected ServletContext context; 

    protected DTO dto;
    
    /*
     * (non-Javadoc)
     * 
     * @see org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet.ServletContext)
     */
    public void setServletContext(ServletContext context) {
        this.context = context;
    } 

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
     */
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    } 

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
     */
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    } 

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
     */
    public void setSession(Map<String, Object> session) {
        this.session = session;
    } 

	@Override
	public DTO getModel() {
		return dto;
	} 
    
    //<!-- 这个就是获得设备上下文的方法，和struts1一样吧^-^ --> 
    public ApplicationContext getApplicationContext() {
        return WebApplicationContextUtils.getWebApplicationContext(context);
    }
    
    //<!-- 作了个处理，以后需要调用spring的bean，直接调用此方法就可以了 --> 
    public Object getObject(String beanName) {
        return getApplicationContext().getBean(beanName);
    }
    
   /**
     *传入json参数
	 *把json转换成字符串
     *加入密匙转换成MD5密码
     *再把json字符串二进制化
     */
    public String sendMassage(JSONObject json,String reStr){
    	String param = json.toString();
    	String pwd = RechargeUtil.encoderByMd5(param +"1234");
    	byte[] bys = param.getBytes();
    	ActionContext ct=ActionContext.getContext();
    	ct.put("param", bys);
    	ct.put("pwd", pwd);
    	return reStr;
    }
    
  
}
